﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace IntoNails.Models
{
    public enum TicketStatus
    {
        Open,
        Closed,
        Cancelled,
        TimeBlocks
    }

    [Serializable]
    public class Ticket : AbstractModel<Guid>, ISerializable
    {
        #region Fields

        private TicketStatus _status;

        #endregion

        #region Constructors

        protected Ticket()
        {
        }

        public Ticket(SerializationInfo info, StreamingContext context)
        {
            this.UniqueCode = (Guid) info.GetValue("UniqueCode", typeof (Guid));
            this.DisplayName = info.GetString("DisplayName");
            this.Status = (TicketStatus)info.GetValue("Status", typeof(TicketStatus));
            this.CreatedDate = info.GetDateTime("CreatedDate");
            this.UpdatedDate = info.GetDateTime("UpdatedDate");
            this.IsValid = info.GetBoolean("IsValid");
        }

        #endregion

        public static Ticket CreateNewTicket()
        {
            return new Ticket
            {
                UniqueCode = Guid.NewGuid(),
                DisplayName = string.Empty,
                Status = TicketStatus.Open,
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                IsValid = true
            };
        }

        public static Ticket CreateTicket(string ticketName)
        {
            return new Ticket
            {
                UniqueCode = Guid.NewGuid(),
                DisplayName = ticketName,
                Status = TicketStatus.Open,
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                IsValid = true
            };
        }

        #region Properties

        public TicketStatus Status
        {
            get { return this._status; }
            set
            {
                this._status = value;
                base.RaisePropertyChanged("Status");
            }
        }

        #endregion

        #region Implementation of ISerializable

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("UniqueCode", this.UniqueCode);
            info.AddValue("DisplayName", this.DisplayName);
            info.AddValue("Status", this.Status);
            info.AddValue("CreatedDate", this.CreatedDate);
            info.AddValue("UpdatedDate", this.UpdatedDate);
            info.AddValue("IsValid", this.IsValid);
        }

        #endregion

        #region Object Overriden

        public override string ToString()
        {
            return string.Format("Ticket ID: {0} | Date: {1}", this.UniqueCode, this.CreatedDate);
        }

        public override int GetHashCode()
        {
            int hashCode = 0;

            unchecked
            {
                hashCode += this.UniqueCode.GetHashCode();
                hashCode += this.Status.GetHashCode();
                hashCode += this.IsValid.GetHashCode();
            }

            return hashCode;
        }

        public override bool Equals(object obj)
        {
            Ticket other = obj as Ticket;
            if (other == null)
            {
                return false;
            }

            return this.UniqueCode == other.UniqueCode;
        }

        #endregion
    }
}
